/**
 * Created by CodeTlan.
 * User: Ing. Miguel Salas
 * Date: 3/6/12
 * Time: 1:35 PM
 */
Ext.define('CT.view.login.Container', {
    extend:'Ext.container.Container',
    alias:'widget.logincontainer',
    region:'north',

    style:{
        backgroundColor:'#7c0000',
        color:'#FFFFFF'
    },
    layout:{
        type:'hbox',
        pack:'end'
    },
    defaults:{
        xtype:'textfield',
        margin:5,
        allowBlank:false,
        vtype:'alphanum'
    },
    initComponent:function () {
        Ext.apply(this, {
            items:[
                {
                    fieldLabel:'Usuario',
                    id:'txtUser',
                    labelWidth:50,
                    minLength:6
                },
                {
                    fieldLabel:'Password',
                    inputType:'password',
                    labelWidth:60,
                    id:'txtPass',
                    minLength:6,
                    listeners:{
                        scope:this,
                        specialkey: function(field, e){
                            if(e.getKey() == e.ENTER) {
                                this.enviar();

                            }
                        }
                    }
                },
                {
                    xtype:'button',
                    text:'Acceder',
                    itemId:'btnAcceder',
                    handler: this.enviar

                }
            ]
        });
        this.callParent(arguments);
    },
    enviar:function(){
        if(Ext.getCmp('txtUser').isValid()&&Ext.getCmp('txtPass').isValid()){
            var _txtUser=Ext.getCmp('txtUser').value;
            var _txtPass=hex_md5(Ext.getCmp('txtPass').value);
            Ext.Ajax.request({
                url: 'index.php/logins/login',
                params: {
                    u:_txtUser,
                    p:_txtPass
                },
                success: function(response){
                    var r = Ext.decode(response.responseText);
                    if(r.success)
                        location.href="index.php";
                    else{
                        Notification.warn(r.error);
                    }

                    // process server response here
                }
            });

        }
    }
});